package com.yunhe.policyfulfillment.authority.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.policyfulfillment.authority.domain.PolicyCompanyRegistration;
import com.yunhe.policyfulfillment.authority.domain.PolicyCompanyUser;
import com.yunhe.policyfulfillment.authority.domain.PolicyUser;
import com.yunhe.policyfulfillment.authority.model.CurrentUser;
import com.yunhe.policyfulfillment.authority.model.UserLogin;
import com.yunhe.policyfulfillment.authority.service.IPolicyUserService;
import com.yunhe.policyfulfillment.authority.service.PolicyCompanyRegistrationService;
import com.yunhe.policyfulfillment.authority.service.PolicyCompanyUserService;
import com.yunhe.policyfulfillment.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LoginController
 * @Description 用户登录
 * @Author HeTao
 * @Date 2021/7/21 15:28
 * @Version 1.0
 **/
@Api(value = "UserLogin", description = "用户登录", tags = {"Model_User_Login"})
@RestController
@RequestMapping(value = "/login")
public class LoginController {

    @Autowired
    private IPolicyUserService policyUserService;

    @Autowired
    private PolicyCompanyUserService policyCompanyUserService;

    @Autowired
    private PolicyCompanyRegistrationService policyCompanyRegistrationService;

    @ApiOperation("后台用户登录")
    @PostMapping(value = "/backPolicyUserLogin")
    public ResponseEntity<ResultObject<Map>> backPolicyUserlLogin(@RequestBody UserLogin userLogin) throws ArgumentErrorException {
        Map<String,Object> map = new HashMap<>();
        CurrentUser currentUser = new CurrentUser();
        PolicyUser policyUser = policyUserService.backPolicyUserlLogin(userLogin);
        if(policyUser == null){
            throw new ArgumentErrorException("用户名或密码错误,请重新输入!");
        }
        if(!"01".equals(policyUser.getActivity())){
            throw new ArgumentErrorException("当前用户已被禁用,请联系管理人员进行恢复！");
        }
        BeanUtils.copyProperties(policyUser, currentUser);
        currentUser.setUserId(String.valueOf(policyUser.getId()));
        currentUser.setRoleName(policyUser.getRoleName());
        currentUser.setRoleId(String.valueOf(policyUser.getRoleId()));
        currentUser.setRoleTitle(policyUser.getRoleTitle());
        currentUser.setOrgId(policyUser.getOrgId());
        String token  = JwtUtil.getToken(policyUser);
        map.put("user",currentUser);
        map.put("token",token);
        return new ResponseEntity<>(new ResultObject(map), HttpStatus.OK);
    }

    /**
     * 新增
     * @author misty
     * @date 2021/09/18
     **/
    @ApiOperation("企业用户注册")
    @PostMapping("/createPolicyCompanyRegistration")
    public ResponseEntity<ResultObject> createPolicyCompanyRegistration(@RequestBody PolicyCompanyRegistration policyCompanyRegistration) throws ArgumentErrorException {
        policyCompanyRegistrationService.createPolicyCompanyRegistration(policyCompanyRegistration);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("门户登录")
    @PostMapping(value = "/portalLogin")
    public ResponseEntity<ResultObject<Map>> portalLogin(@RequestBody UserLogin userLogin) throws ObjectNotFoundException, ArgumentErrorException {
        Map<String,Object> map = new HashMap<>(2);
        CurrentUser currentUser = new CurrentUser();
        PolicyCompanyUser policyCompanyUser = policyCompanyUserService.login(userLogin);
        BeanUtils.copyProperties(policyCompanyUser, currentUser);
        currentUser.setUserId(String.valueOf(policyCompanyUser.getId()));
        String token = JwtUtil.getToken(policyCompanyUser);
        map.put("user", currentUser);
        map.put("token", token);
        return new ResponseEntity<>(new ResultObject(map), HttpStatus.OK);
    }
}
